import requests
import json
import get_token
import readConfig
import get_headers


class get_Auth_Token:
    def get_auth_token(self):  # 获取登录接口返回的auth_token，保存为self.auth_token
        url = readConfig.readConfig('open', 'get_auth_token')
        sn = readConfig.readConfig("data", "sn")
        account = readConfig.readConfig('shop', 'account')  # 商户为shop，店员为cashier
        password = readConfig.readConfig('shop', 'password')
        role_login = readConfig.readConfig('shop', 'role_login')
        token = get_token.get_Token().get_token()[0]
        headers = get_headers.get_headers()
        # url = 'https://open.dairphone.cn/jfdevice/login?ver=1.0&from=hardware'
        data = {
            "sn": sn,
            "account": account,
            "password": password,
            "role_login": role_login,
            "token": token  # 取激活接口返回的token用作登录，目的是拿到返回的auth_token
        }
        result2 = requests.post(url, data=data, headers=headers)
        results2 = result2.json()
        print(json.dumps(results2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        try:
            auth_token = results2['data']['auth_token']
        except Exception:
            print(Exception)
            print(json.dumps(results2, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False))
        else:
            # print(auth_token)
            return auth_token
